
var chat = {
    'face_img_uri':'/static/m/images/QQexpression/',
    "online":function(data){
        var error = data.error;
        var msg = data.msg;
    },
    "show_time":function(data){
        //to uid,和谁聊天
        var to_uid = data.to_uid;
        var msg_time_key = "msg_time_"+to_uid;
        var msg_time = jq.cookie(msg_time_key);
        var time = data.time;
        var pre_date = new Date(time*1000); //毫秒
        var show_time = pre_date.getHours()+":"+pre_date.getMinutes()+":"+pre_date.getSeconds();
        var show_time_str = "今天&nbsp;"+show_time;
        var return_msg = {"show":0,"show_str":show_time_str};

        if(typeof(msg_time) == "undefined"){
            jq.cookie(msg_time_key,time);
            return_msg.show = 1;
        }else{
            var msg_time_interval = 300;

            if(( time - msg_time ) >= msg_time_interval){  //秒
                jq.cookie(msg_time_key,time);
                return_msg.show = 1;
            }

        }

        return return_msg;
    },
    "get_msg_showtime":function(data){
        //to uid,和谁聊天
        var to_uid = data.from_uid;
        var msg_time_key = "msg_time_"+to_uid;
        var msg_time = jq.cookie(msg_time_key);
        var time = data.time;
        var pre_date = new Date(time*1000); //毫秒
        var show_time = pre_date.getHours()+":"+pre_date.getMinutes()+":"+pre_date.getSeconds();
        var show_time_str = "今天&nbsp;"+show_time;
        var return_msg = {"show":0,"show_str":show_time_str};

        if(typeof(msg_time) == "undefined"){
            jq.cookie(msg_time_key,time);
            return_msg.show = 1;
        }else{
            var msg_time_interval = 300;

            if(( time - msg_time ) >= msg_time_interval){  //秒
                jq.cookie(msg_time_key,time);
                return_msg.show = 1;
            }

        }

        return return_msg;
    },
    //发送消息
    "push_msg": function(data,ws){

        var msg = jq.trim(jq("#msg-text").val());
        var to_uid = jq("BODY").attr("uid");
        var action = "push_msg";
        var from_uid = jq("BODY").attr("current_uid");

        if(msg.length == 0){
            alert("说点什么吧?");
            return false;
        }

        var push_data = new Object();
        push_data.action = action;
        push_data.data = new Object();
        push_data.data.from_uid = from_uid;
        push_data.data.to_uid = to_uid;
        push_data.data.msg = msg;
        websocket.send(jq.toJSON(push_data));
        jq("#msg-text").val('');
    },
    //转换消息中的表情
    "tranformation_expresson":function(msg){
        //匹配表情
        var msgVal = msg;
        var msgInputValArr = msgVal.match(/\[\d+\]/g);
        var marrlen = 0;
        if(msgInputValArr != null){
            marrlen = msgInputValArr.length;
            if(marrlen>0){
                for(var m=0; m<marrlen; m++){
                    var picnum = msgInputValArr[m].substring(1,msgInputValArr[m].length-1);
                    var picurl = "<img src='"+chat.face_img_uri+picnum+".gif' class='d-face'>";
                    msgVal = msgVal.replace("["+picnum+"]",picurl);
                }
            }
        }
        return msgVal;
    },
    //收到消息
    "get_msg":function(data,ws){
        //获取消息

        var msg = data.msg;
        var error = data.error;
        var from_uid = data.from_uid;
        var time = data.time;
        //var type = data.type;

        var show_msg = chat.tranformation_expresson(msg);
        var check_msg_time = chat.get_msg_showtime(data);

        var other_msg = '<div class="msg-item item.senderself" msg-type=\'item.type\' msg-content=\'item.content\'>';

        if(check_msg_time.show == 1){
            other_msg+='<div class="mgs-time"><time>'+check_msg_time.show_str+'</time></div>';
        }

        other_msg+='<a href="javascript:;"><img class="msg-user-img" src="'+to_user_cover+'" alt="" /></a>';
        other_msg+='<div class="msg-content"><div class="msg-content-inner">'+show_msg+'</div><div class="msg-content-arrow"></div></div>';
        other_msg+='<div class="mui-item-clear"></div></div>';

        jq("#msg-list").append(other_msg).scrollTop(jq('#msg-list').height() + 300);
        chat.read_msg(data,ws);

    },
    "read_msg":function(data,ws){
        var action = "read_msg";
        var from_uid = jq("BODY").attr("uid");
        var to_uid = jq("BODY").attr("current_uid");

        var push_data = new Object();
        push_data.action = action;
        push_data.data = new Object();
        push_data.data.from_uid = from_uid; //谁发给我的
        push_data.data.to_uid = to_uid;  // 自己
        websocket.send(jq.toJSON(push_data));
    },
    //收到服务端发送确认后再显示消息
    "show_send_msg": function(data,ws){
        //2016年08月20日 10:33:32
        var time = data.time;
        var msg = data.msg;
        var show_msg = chat.tranformation_expresson(msg);
        var check_msg_time = chat.show_time(data);
        //console.log(check_msg_time);

        var me_div = '<div class="msg-item msg-item-self" msg-type=\'item.type\' msg-content=\'item.content\'>';

        if(check_msg_time.show == 1){
            me_div+='<div class="mgs-time"><time>'+check_msg_time.show_str+'</time></div>';
        }

        me_div+='<img class="msg-user msg-user-img" src="'+from_user_cover+'" alt="" />';
        me_div+='<div class="msg-content"><div class="msg-content-inner">';
        me_div+=show_msg;
        me_div+='</div><div class="msg-content-arrow"></div></div><div class="mui-item-clear"></div></div>';

        jq("#msg-list").append(me_div).scrollTop(jq('#msg-list').height() + 300);

    }

};
